package com.ll.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*
 * 初始化RabbitMq 队列、交换机、绑定
 * 只需要@Bean 在容器启动的时候就会创建（Rabbit没有的情况）
 * 如果属性改变了、则再次创建不会生效，需要删除在创建
 */
@Configuration
public class RabbitMqInit {

    @Bean
    public Queue orderDelayQueue() {

        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //@Nullable Map<String, Object> arguments
        //设置属性
        Map<String, Object> arguments = new HashMap<>();
        //x-dead-letter-exchange 时间到了去那个交换机
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        //x-dead-letter-routing-key 时间到了使用那个路由键
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        //延时时间 ms单位
        arguments.put("x-message-ttl", 60000);

        Queue queue = new Queue("order.delay.queue",
                true,
                false,
                false,
                arguments);
        return queue;
    }


    @Bean
    public Queue orderRelaseOrderQueue() {
        Queue queue = new Queue("order.release.order",
                true,
                false,
                false,
                null);
        return queue;
    }

    @Bean
    public Exchange orderEventExchange() {
        //e(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("order-event-exchange",
                true, false,
                null);
        return topicExchange;
    }


    @Bean
    public Binding orderDelayQueueBinding() {

        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			@Nullable Map<String, Object> argument
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderRelaseOrderQueueBinding() {

        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			@Nullable Map<String, Object> argument
        return new Binding("order.release.order",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    //订单释放和库存释放绑定  让订单一释放库存就可以感知
    @Bean
    public Binding orderReleaseBindingWareRelease() {

        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			@Nullable Map<String, Object> argument
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }
}
